package com.zhu.service.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zhu.service.edu.mapper.EduCourseMapper;
import com.zhu.service.edu.mapper.EduTeacherMapper;
import com.zhu.service.edu.pojo.EduCourse;
import com.zhu.service.edu.pojo.EduCourseCollect;
import com.zhu.service.edu.mapper.EduCourseCollectMapper;
import com.zhu.service.edu.pojo.EduTeacher;
import com.zhu.service.edu.pojo.vo.CourseCollectVo;
import com.zhu.service.edu.service.IEduCourseCollectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程收藏 服务实现类
 * </p>
 *
 * @author zhu
 * @since 2022-07-04
 */
@Service
public class EduCourseCollectServiceImpl extends ServiceImpl<EduCourseCollectMapper, EduCourseCollect> implements IEduCourseCollectService {
     @Autowired
     private EduCourseCollectMapper courseCollectMapper;
    @Override
    public boolean removeCollect(String courseId, String id) {
        LambdaQueryWrapper<EduCourseCollect> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(EduCourseCollect::getCourseId,courseId);
        queryWrapper.eq(EduCourseCollect::getMemberId,id);
        int delete = courseCollectMapper.delete(queryWrapper);
        if(delete>0){
            return true;
        }
        return false;
    }
    @Autowired
    private EduCourseMapper courseMapper;
    @Autowired
    private EduTeacherMapper teacherMapper;

    @Override
    public List<CourseCollectVo> collectList(String id) {
        LambdaQueryWrapper<EduCourseCollect> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(EduCourseCollect::getMemberId,id);
        List<EduCourseCollect> eduCourseCollects = courseCollectMapper.selectList(queryWrapper);
        List<CourseCollectVo> courseCollectVoList=new ArrayList<>();
        for (EduCourseCollect eduCourseCollect : eduCourseCollects) {
            String courseId = eduCourseCollect.getCourseId();
            EduCourse course = courseMapper.selectById(courseId);
            String teacherId = course.getTeacherId();
            EduTeacher eduTeacher = teacherMapper.selectById(teacherId);
            CourseCollectVo courseCollectVo=new CourseCollectVo();
            courseCollectVo.setCover(course.getCover());
            courseCollectVo.setGmtCreate(eduCourseCollect.getGmtCreate());
            courseCollectVo.setId(eduCourseCollect.getId());
            courseCollectVo.setLessonNum(course.getLessonNum());
            courseCollectVo.setPrice(course.getPrice());
            courseCollectVo.setTeacherName(eduTeacher.getName());
            courseCollectVo.setTitle(course.getTitle());
            courseCollectVoList.add(courseCollectVo);
        }
        return courseCollectVoList;
    }

    @Override
    public Boolean isCollect(String id, String courseId) {
        LambdaQueryWrapper<EduCourseCollect> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(EduCourseCollect::getCourseId,courseId);
        queryWrapper.eq(EduCourseCollect::getMemberId,id);
        EduCourseCollect courseCollect = courseCollectMapper.selectOne(queryWrapper);
        if(courseCollect!=null){
            return true;
        }
        return false;
    }
}
